package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.Product;
import com.atguigu.paymentdemo.entity.RefundInfo;
import com.atguigu.paymentdemo.enums.OrderStatus;
import com.atguigu.paymentdemo.enums.PayType;
import com.atguigu.paymentdemo.enums.wxpay.WxRefundStatus;
import com.atguigu.paymentdemo.mapper.OrderInfoMapper;
import com.atguigu.paymentdemo.mapper.ProductMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.util.OrderNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * @Description: 创建订单
     * @param productId 商品ID
     * @return  orderInfo
     */
    @Override
    public OrderInfo createOrderByProductId(Long productId,String payType) {

        OrderInfo orderInfo = getNoPayOrderByProductId(productId,payType);
        if(orderInfo!=null){
            return orderInfo;
        }
        orderInfo = new OrderInfo();
        Product product = productMapper.selectById(productId);
        orderInfo.setTitle(product.getTitle());
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
        orderInfo.setProductId(productId);
        //1分钱
        orderInfo.setTotalFee(product.getPrice());
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        orderInfo.setPaymentType(payType);
        int insert = baseMapper.insert(orderInfo);
        return orderInfo;
    }

    private OrderInfo getNoPayOrderByProductId(Long productId,String payType) {
        LambdaQueryWrapper<OrderInfo> orderInfoQueryWrapper = new LambdaQueryWrapper<>();
        orderInfoQueryWrapper.eq(OrderInfo::getProductId,productId)
                .eq(OrderInfo::getPaymentType,payType)
                .eq(OrderInfo::getOrderStatus,OrderStatus.NOTPAY.getType());
        return baseMapper.selectOne(orderInfoQueryWrapper);
    }

    /**
     * @Description: 保存支付二维码
     * @param orderNo  订单编号
     * @param codeUrl   二维码链接
     */
    public void saveCodeUrl(String orderNo,String codeUrl){
        LambdaQueryWrapper<OrderInfo> orderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderInfoLambdaQueryWrapper.eq(OrderInfo::getOrderNo,orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);

        baseMapper.update(orderInfo,orderInfoLambdaQueryWrapper);
    }

    /**
     * @Description: 倒叙查询订单列表
     * @return  订单集合
     */
    @Override
    public List<OrderInfo> listOrderInfoByCreateTimeDesc() {
        LambdaQueryWrapper<OrderInfo> orderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderInfoLambdaQueryWrapper.orderByDesc(OrderInfo::getCreateTime);
        return baseMapper.selectList(orderInfoLambdaQueryWrapper);
    }


    /**
     * @Description: 更新订单状态
     * @param outTradeNo    订单号
     * @param status       状态
     */
    @Override
    public void updateStausByOrderNo(Object outTradeNo, OrderStatus status) {
        log.info("更新订单状态");
        LambdaQueryWrapper<OrderInfo> orderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderInfoLambdaQueryWrapper.eq(OrderInfo::getOrderNo,outTradeNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(status.getType());

        baseMapper.update(orderInfo,orderInfoLambdaQueryWrapper);

    }

    @Override
    public String getOrderStatus(Object outTradeNo) {
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<OrderInfo>();
        lambdaQueryWrapper.eq(OrderInfo::getOrderNo,outTradeNo);
        OrderInfo orderInfo = baseMapper.selectOne(lambdaQueryWrapper);
        if(Objects.isNull(orderInfo)){
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    /**
     * @Description: 查询超过mimutes，而未支付的订单
     * @param minutes
     * @return
     */
    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes) {
        Instant time = Instant.now().minus(Duration.ofMinutes(minutes));
        LambdaQueryWrapper<OrderInfo> orderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderInfoLambdaQueryWrapper.eq(OrderInfo::getOrderStatus,OrderStatus.NOTPAY.getType())
                .lt(OrderInfo::getCreateTime,time);
        List<OrderInfo> orderInfoList = baseMapper.selectList(orderInfoLambdaQueryWrapper);
        return orderInfoList;
    }

    /**
     * @Description: 获取订单信息
     * @param orderNo   订单号
     * @return  订单信息
     */
    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {
        LambdaQueryWrapper<OrderInfo> orderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderInfoLambdaQueryWrapper.eq(OrderInfo::getOrderNo,orderNo);
        return baseMapper.selectOne(orderInfoLambdaQueryWrapper);
    }


}
